package per.mnxc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import per.mnxc.common.R;
import per.mnxc.pojo.Column;
import per.mnxc.mapper.ColumnMapper;
import per.mnxc.service.IColumnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mnxc
 * @since 2022-11-26
 */
@Service
public class ColumnServiceImpl extends ServiceImpl<ColumnMapper, Column> implements IColumnService {

    @Autowired
    ColumnMapper columnMapper;

    @Override
    public R<String> removeLeavesByIds(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            List<Column> children = columnMapper.getChildren(ids.get(i));
            if(children!=null) return R.error("类型删除失败，只能删除叶子节点");
        }
        this.removeByIds(ids);
        return R.success("删除成功");
    }

    @Override
    public R<IPage<Column>> page(int page, int pageSize, String name) {
        //1.分页构造器
        IPage<Column> pageInfo = new Page<>(page,pageSize);
        //2.条件构造器
        LambdaQueryWrapper<Column> lqw = new LambdaQueryWrapper<>();
        lqw.like(name!=null,Column::getTypename,name);
        //3.查询数据
        this.page(pageInfo,lqw);
        return R.success(pageInfo);
    }

    @Override
    public R<List<Column>> getAll() {
        List<Column> columnList = columnMapper.getAll();
        return R.success(columnList);
    }
}
